package top.xiaoazi.futureletterserver.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.xiaoazi.futureletterserver.common.R;
import top.xiaoazi.futureletterserver.exception.ConnectionTimeoutException;


@Component
public class RequestUtils {
    @Autowired
    private ObjectMapper objectMapper;

    public final Integer BAD_REQUEST_STATE = 400; //非法请求
    public final String BAD_REQUEST_MESSAGE = "非法请求。";

    public final Integer NOT_LOGIN_STATE = 401; // 未登录或登录过期状态码
    public final String NOT_LOGIN_MESSAGE = "登录过期或者未登录，请重新登录。"; // 未登录或登录过期消息

    public final Integer NOT_ACCESS_STATE = 403; // 用户无权限
    public final String NOT_ACCESS_MESSAGE = "该模块您无使用权限，请联系管理员开通！"; // 用户无权限

    // 用户存在
    public final Integer USER_EXISTS_STATE = 206;
    public final String USER_EXISTS_MESSAGE = "该用户名已存在，请更换用户名注册！";

    // 邮箱存在
    public final Integer EMAIL_EXISTS_STATE = 206;
    public final String EMAIL_EXISTS_MESSAGE = "该邮箱已被注册，请更换邮箱注册！";

    // 验证码过期
    public final Integer AUTH_CODE_PASS_STATE = 206;
    public final String AUTH_CODE_PASS_MESSAGE = "验证码过期，请重新获取。";

    // 验证码错误
    public final Integer AUTH_CODE_ERROR_STATE = 206;
    public final String AUTH_CODE_ERROR_MESSAGE = "验证码错误，请重新提交。";

    // 频繁提交
    public final Integer FREQUENT_SUBMISSION_STATE = 201;
    public final String FREQUENT_SUBMISSION_MESSAGE= "提交太快了，请过一小会再提交哦！";

    // 校验不通过通用状态码
    public final Integer NO_PASS_COMMON_STATE = 203;



    // Python 服务器接口根路径
    public final String PYTHON_SERVER_BASE_PATH = "http://127.0.0.1:7777/script/csdn/";
    /**
     * post 请求
     * @param url
     * @param data
     * @return
     */
    public R post(String url, Object data) {
        // 开启 http 客户端，使用 try-with-resources 语句
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 将 Blog 对象转换为 JSON 字符串
            String jsonString = objectMapper.writeValueAsString(data);

            // 创建 HttpPost 请求
            HttpPost httpPost = new HttpPost(url);

            // 设置请求头，告诉服务器发送的是 JSON 格式的数据
            httpPost.setHeader("Content-type", "application/json");

            // 设置请求体为 JSON 字符串
            StringEntity entity = new StringEntity(jsonString);
            httpPost.setEntity(entity);
            String res = EntityUtils.toString(httpClient.execute(httpPost).getEntity(), "UTF-8");
            return objectMapper.readValue(res, R.class);
            // 注意：由于使用了 try-with-resources，httpClient 会在 try 块结束时自动关闭
        } catch (Exception e) {
            throw new ConnectionTimeoutException();
        }
    }

    /**
     * get 请求
     * @param url
     * @return
     */
    public R get(String url) {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 创建 HttpGet 请求
            HttpGet httpGet = new HttpGet(url);
            // 调用 HttpClient 的 execute 方法执行请求
            String res = EntityUtils.toString(httpClient.execute(httpGet).getEntity(), "UTF-8");
            return objectMapper.readValue(res, R.class);

            // 注意：由于使用了 try-with-resources，httpClient 会在 try 块结束时自动关闭
        } catch (Exception e) {
            throw new ConnectionTimeoutException();
        }
    }
}
